package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pojo.Order;
import com.itheima.vo.OrderInfoVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @author ：yuanjian
 * @date ：Created in 2020/11/17 17:01
 * @description：
 */
public interface OrderMapper extends BaseMapper<Order> {
    @Select("select count(1) from t_order where member_id = #{memberId} and order_date = #{orderDate} and setmeal_id = #{setmealId}")
    int hasOrderByOrderDateAndSetmealIdAndMemberId(@Param("orderDate") String orderDate, @Param("setmealId") Long setmealId, @Param("memberId") Long memberId);

    @Select("select tm.`name` member,ts.`name` setmeal,tor.`order_date`,tor.`order_type`,tor.`order_no`  \n" +
            "from `t_member` tm,`t_order` tor,`t_setmeal` ts\n" +
            "where tm.`id`=tor.`member_id` and ts.`id`=tor.`setmeal_id` and tor.`id`=#{orderId}")
    OrderInfoVo findOrderInfo(Long orderId);

    @Update("update `t_order` set pay_status=1 where order_no = #{orderNo}")
    void updateStatus(String orderNo);

    @Select("select pay_status from `t_order` where order_no = #{orderNo}")
    String isPay(String orderNo);


    @Select("select pay_status from t_order where id=#{orderId}")
    Integer selectPayStatusById(Long orderId);

    @Select("SELECT s.name, COUNT(o.setmeal_id) value\n" +
            "FROM t_order o left join t_setmeal s \n" +
            "ON o.setmeal_id = s.id GROUP BY o.setmeal_id")
    List<Map> findsetmeal();

    /**
     * 各个套餐预约数
     * */
    @Select("select s.name ,count(*) as value\n" +
            "from t_order o\n" +
            "left join t_setmeal s on o.setmeal_id = s.id\n" +
            "group by setmeal_id")
    public List<Map> selectSetmealPercent();


    @Select("select count(id) from t_order where order_date=#{today}")
        //今日预约数
    Integer findOrderCountByDate(String today);

    @Select("select count(id) from t_order where order_date=#{today} and pay_status=2")
        //今日到诊数
    Integer findVisitsCountByDate(String today);

    @Select("select count(id) from t_order where order_date >=#{thisweekMonday}")
        //根据日期统计预约数，统计指定日期之后的预约数
    Integer findOrderCountAfterDate(String thisweekMonday);

    @Select("select count(id) from t_order where order_date >=#{thisweekMonday} and pay_status=2 ")
        //根据日期统计到诊数，统计指定日期之后的到诊数
    Integer findVisitsCountAfterDate(String thisweekMonday);


    @Select("select s.name,count(o.id) setmeal_count, count(o.id)/(select count(id) from t_order)*100 proportion\n" +
            "        from t_order o,t_setmeal s where\n" +
            "        o.setmeal_id=s.id\n" +
            "        group by o.setmeal_id\n" +
            "        order by setmeal_count desc\n" +
            "        limit 3")
        //热门套餐
    List<Map> findHotSetmeal();
}
